﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace lcsign.Web.documentTransfer
{
    /// <summary>
    /// DocOp 的摘要说明
    /// </summary>
    public class DocOp : IHttpHandler, IRequiresSessionState
    {

        lcsign.BLL.documentinfo bll = new lcsign.BLL.documentinfo();
        Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            string did = "";
            string uid = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (null != context.Request.Form["id"])
            {//获取前台传来的值  
                did = context.Request.Form["id"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["id"])
            {
                did = context.Request.QueryString["id"].ToString().Trim();
            }

            if (null != context.Request.Form["uid"])
            {//获取前台传来的值  
                uid = context.Request.Form["uid"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["uid"])
            {
                uid = context.Request.QueryString["uid"].ToString().Trim();
            }
           

            if (ac == "query") {
                Query(context, did);
            }
            if (ac == "submitDoc")
            {
                SubmitDoc(context,did);
            } 
            if (ac == "querymy")
            {
                Querymy(context);
            }
            if (ac == "check_ok")
            {
                CheckOk(context,did,uid);
            }
            if (ac == "check_no")
            {
                CheckNo(context, did, uid);
            }
        
               
        }

        private void CheckNo(HttpContext context, string did, string uid) 
        {
            DataSet ds_doc = bll.GetList(" did= " + did);

            if (ds_doc.Tables[0].Rows.Count == 1)
            {
                int dpid = Convert.ToInt32(did);
                int person_id = Convert.ToInt32(uid);
                string reason = "否决";

                //返回上一步
                Maticsoft.BLL.Tools.DealDisagreeLast(dpid, person_id, reason);

                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F_DI01");//返回给前台页面  
                context.Response.End();
            }
           
            

          
        
        }

        //审核通过
        private void CheckOk(HttpContext context,string did,string uid)
        {
            DataSet ds_doc = bll.GetList(" did= " + did);

            if (ds_doc.Tables[0].Rows.Count == 1)
            {
                int dpid = Convert.ToInt32(did);
                int person_id = Convert.ToInt32(uid);

                string reason = "同意";

                //同意处理
                Maticsoft.BLL.Tools.DealAgree(dpid, person_id, reason);

                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F_DI01");//返回给前台页面  
                context.Response.End();
            }

            

            
        }


        //返回数据
        private void Query(HttpContext context, string did)
        {
            if (!string.IsNullOrEmpty(did))
            {
                DataSet ds = bll.GetList(" did= " + did);

                if (ds.Tables[0].Rows.Count == 1)
                {
                    context.Response.ContentType = "text/json";

                    string strJson = ToJson.DataTable2JsonNormal(ds.Tables[0]);//DataSet数据转化为Json数据                
                    context.Response.Write(strJson);//返回给前台页面  
                    context.Response.End();
                }


            }
        }
        //绑定表格
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                DataSet check = new Maticsoft.BLL.workFlow().GetDaiBanCheck("0", uid.ToString(), 1, "审核");

                string strJson = ToJson.Dataset2Json(check, -1, -1);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        
        //提交审核
        private void SubmitDoc(HttpContext context, string did)
        {
            if (!string.IsNullOrEmpty(did))
            {
                DataSet ds_doc = bll.GetList(" did= " + did);

                if (ds_doc.Tables[0].Rows.Count == 1)
                {
                    int dpid = Convert.ToInt32(did);
                    int person_id = Convert.ToInt32(ds_doc.Tables[0].Rows[0]["uid"]);

                    Maticsoft.BLL.documentFlowInfo dfm = new Maticsoft.BLL.documentFlowInfo();
                    List<Maticsoft.Model.documentFlowInfo> li = dfm.GetModelList(" document_id = " + dpid);

                    if (li.Count == 1)
                    {
                        Maticsoft.Model.documentFlowInfo d = li[0];

                        d.status = 1;
                        dfm.Update(d);//更改状态为审批中

                        DataSet ds = wm.GetWorkFlow(d.flow_id.Value, 1);

                        if (ds.Tables[0].Rows.Count > 0)//判断0节点处是否需要会签
                        {
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                Maticsoft.Model.workFlow w = new Maticsoft.Model.workFlow();
                                w.document_id = d.document_id;
                                w.flow_node_id = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_id"].ToString());
                                w.flow_node_num = Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_num"].ToString());
                                w.person_id = Convert.ToInt32(ds.Tables[0].Rows[i]["person_id"].ToString());
                                w.workFlow_direction = 0;//代表此节点是正向，非反向（退签）
                                w.workFlow_reson = "";
                                w.workFlow_status = 0;//审批状态(0、未审批，1、同意，2、否决(退回首节点))
                                w.workFlow_time = DateTime.Now;//提交日期
                                if (Convert.ToInt32(ds.Tables[0].Rows[i]["flow_node_isAutoNext"]) == 0)
                                {
                                    w.workFlow_isWork = false;
                                }
                                else
                                {
                                    w.workFlow_isWork = true;
                                }
                                w.workFlow_submitId = person_id;

                                new Maticsoft.BLL.workFlow().Add(w);//添加一条新流程

                            }

                            context.Response.Write("T");//返回给前台页面  
                            context.Response.End();
                        }
                        else
                        {
                            context.Response.Write("F_WF01");//返回给前台页面  
                            context.Response.End();
                        }

                    }
                    else
                    {
                        context.Response.Write("F_DFI01");//返回给前台页面  
                        context.Response.End();
                    }


                }
                else
                {
                    context.Response.Write("F_DI01");//返回给前台页面  
                    context.Response.End();
                }
            
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


       
    }
}